import java.util.*;

/**
 * 合并两个二维数组-求和法
 *
 * @USER: Administrator
 * @DATE: 2024/10/5 上午10:03
 * @VERSION: 1.0.0
 */
public class LC2570 {
    public static void main(String[] args) {
        LC2570 lc2570 = new LC2570();
        System.out.println(Arrays.deepToString(lc2570.mergeArrays(new int[][]{{1,2},{2,3},{4,5}},new int[][]{{1,4},{3,2},{4,1}})));
    }

    public int[][] mergeArrays(int[][] nums1, int[][] nums2) {
        List<int[]> list = new ArrayList<>(nums1.length + nums2.length);
        Set<Integer> set = new HashSet<>(nums1.length + nums2.length);
        for (int[] ints : nums1) {
            int[] arr = new int[2];
            arr[0] = ints[0];
            arr[1] = ints[1] + getValueById(nums2, ints[0]);
            list.add(arr);
            set.add(arr[0]);
        }
        for (int[] ints : nums2) {
            if (set.contains(ints[0])) {
                continue;
            }
            int[] arr = new int[2];
            arr[0] = ints[0];
            arr[1] = ints[1] + getValueById(nums1, ints[0]);
            list.add(arr);
        }

        // 排序
        int[][] array = list.toArray(new int[list.size()][]);
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length-1; j++) {
                if (array[j][0] > array[j+1][0]) {
                    int[] temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                }
            }
        }
        return array;



    }

    public int getValueById(int[][] nums, int id){
        for (int[] num : nums) {
            if (num[0] == id) return num[1];
        }
        return 0;
    }

}
